/*
 * trap.c - handle trap gates
 * liuw
 * 16-Jun-08
 */
/*
 * note: page fault handler should be in MM, not here
 */
#include <init/dt.h>
#include <init/isr.h>

static void die(char *str, registers_t r)
{

	printk("%s: %04x\n", str, r.err_code&0xffff);
	printk("EIP: %04x:%p\nEFLAGS: %p\nESP: %04x:%p\n", 
			r.cs, r.eip, r.eflags, r.ss, r.esp);
	for(;;){}
}

void do_divide_error(registers_t r)
{
	die("divide error", r);
}

void do_debug(registers_t r)
{
	die("debug", r);
}

void do_nmi(registers_t r)
{
	die("nmi", r);
}

void do_int3(registers_t r)
{
	die("int3", r);
}


void do_overflow(registers_t r)
{
	die("overflow", r);
}

void do_bounds(registers_t r)
{
	die("bounds", r);
}

void do_invalid_operand(registers_t r)
{
	die("invalid operand", r);
}

void do_device_not_available(registers_t r)
{
	die("device not available", r);
}

void do_double_fault(registers_t r)
{
	die("double fault", r);
}

void do_coprocessor_overrun(registers_t r)
{
	die("coprocessor overrun", r);
}

void do_invalid_TSS(registers_t r)
{
	die("invalid TSS", r);
}

void do_segment_not_present(registers_t r)
{
	die("segment not present", r);
}

void do_stack_error(registers_t r)
{
	die("stack error", r);
}

void do_general_protection(registers_t r)
{
	die("general protection", r);
}

void do_coprocessor_error(registers_t r)
{
	die("coprocessor error", r);
}


extern void do_page_fault(registers_t);

void init_trap(void)
{
	register_interrupt_handler(0, &do_divide_error);
	register_interrupt_handler(1, &do_debug);
	register_interrupt_handler(2, &do_nmi);
	register_interrupt_handler(3, &do_int3);
	register_interrupt_handler(4, &do_overflow);
	register_interrupt_handler(5, &do_bounds);
	register_interrupt_handler(6, &do_invalid_operand);
	register_interrupt_handler(7, &do_device_not_available);
	register_interrupt_handler(8, &do_double_fault);
	register_interrupt_handler(9, &do_coprocessor_overrun);
	register_interrupt_handler(10, &do_invalid_TSS);
	register_interrupt_handler(11, &do_segment_not_present);
	register_interrupt_handler(12, &do_stack_error);
	register_interrupt_handler(13, &do_general_protection);
	// isr14 handler is not implemented here, see mm/memory.c
	register_interrupt_handler(14, &do_page_fault);
	register_interrupt_handler(16, &do_coprocessor_error);
	return;
}
